﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using RenderHeads.Media.AVProVideo.Demos;
using DG.Tweening;
using RenderHeads.Media.AVProVideo;
using UnityEngine.UI;

public class DragEvent_Video : MonoBehaviour, IDragHandler, IBeginDragHandler, IEndDragHandler
{
    private static Transform mediaPlayerUI;
    private Vector3 currentPointPos;
    private Vector3 onBeginPointPos;


    private void Start()
    {
        mediaPlayerUI = GameObject.FindGameObjectWithTag("MediaPlayerUI").GetComponent<Transform>();

        //自动设置按钮监听
        transform.parent.Find("ButtonQuitVideo").GetComponent<Button>().onClick.AddListener(EndPlayVideo);
    }

    public void OnDrag(PointerEventData eventData)
    {
        //Debug.Log(eventData.position.x);
        if (onBeginPointPos.x < 180F)
        {
            currentPointPos = eventData.position;
            mediaPlayerUI.Find("Panel").GetComponent<RectTransform>().localPosition = new Vector2(currentPointPos.x, 0F);
        }
    }

    public void OnBeginDrag(PointerEventData eventData)
    {
        onBeginPointPos = eventData.position;
    }

    public static void EndPlayVideo()
    {
        Tweener tweener = mediaPlayerUI.Find("Panel").GetComponent<RectTransform>().DOLocalMove(new Vector2(1600, 0), 1F);
        GameObject.FindGameObjectWithTag("MediaPlayer").GetComponent<MediaPlayer>().Control.Pause();
        tweener.OnComplete(() => { MediaPlayerUI.canQuit = true; });
    }

    public void OnEndDrag(PointerEventData eventData)
    {
        if (onBeginPointPos.x < 225F)
        {
            if (Vector2.Distance(onBeginPointPos, currentPointPos) > 80F)
            {
                EndPlayVideo();
            }
            else
            {
                mediaPlayerUI.Find("Panel").GetComponent<RectTransform>().DOLocalMove(Vector2.zero, 0.8F);
            }
        }
    }
}